<?php

namespace App\Helpers\Api;
use Illuminate\Database\Eloquent\Builder;

trait SearchTrait
{

    public function scopeApply(Builder $q, $search, $input) {
        foreach ($search as $key => $default_val) {
            if (strpos($key, '.') !== false) {
                $arr = explode(".", $key);
                $new_key = $arr[0] . "_" . $arr[1];
                $val = $input[$new_key] ?? $default_val;
                $method = ucfirst(toCamelCase($arr[1]));
                $q = $q->{$method}($val, $arr[0]);
            } else {
                $method = ucfirst(toCamelCase($key));
                $val = $input[$key] ?? $default_val;
                $q = $q->{$method}($val);
            }
        }
        return $q;
    }

    public function scopeTitle(Builder $q,  $title){
        if ($title)
            return $q->where('title', 'like', '%' . $title . '%');
    }

    public function scopeId(Builder $q,  $val){
        if ($val)
            return $q->where('id', $val);
    }


    public function scopeMessage(Builder $q,  $nickname){
        if ($nickname)
            return $q->where('message', 'like', '%' . $nickname . '%');
    }

    public function scopeStartAt(Builder $q,  $start_at){
        if ($start_at)
            return $q->where('created_at', '>=', $start_at);
    }

    public function scopeEndAt(Builder $q,  $end_at){
        if ($end_at) {
            $end_at = date('Y-m-d', strtotime($end_at) + 24 * 3600);
            return $q->where('created_at', '<', $end_at);
        }
    }

    public function scopeAuditStatus(Builder $q,  $audit_status){
        if ($audit_status != -1)
            return $q->where('audit_status', '=', $audit_status);
    }

    public function scopeIsOn(Builder $q,  $is_on){
        if ($is_on != -1)
            return $q->where('is_on', '=', $is_on);
    }

    public function scopeIsBlack(Builder $q,  $value){
        if ($value != -1)
            return $q->where('is_black', '=', $value);
    }

    public function scopeCategory(Builder $q,  $category_id){
        if ($category_id) {
            $arr_category_id = explode(",", $category_id);
            return $q->whereIn('category_id', $arr_category_id);
        }
    }

    public function scopeCategoryId(Builder $q,  $category_id){
        if ($category_id) {
            $arr_category_id = explode(",", $category_id);
            return $q->whereIn('category_id', $arr_category_id);
        }
    }

    public function scopeParentCategoryId(Builder $q,  $val){
        if ($val) {
            $arr_category_id = explode(",", $val);
            return $q->whereIn('parent_category_id', $arr_category_id);
        }
    }

    public function scopeIsVip(Builder $q,  $is_vip){
        if ($is_vip != -1)
            return $q->where('is_vip', '=', $is_vip);
    }

    public function scopeParentId(Builder $q,  $parent_id){
        if ($parent_id !== null)
            return $q->where('parent_id', '=', $parent_id);
    }

    public function scopeCookBookId(Builder $q,  $cookbook_id){
        if ($cookbook_id)
            return $q->where('cookbook_id', '=', $cookbook_id);
    }

    public function scopeType(Builder $q,  $type){
        if ($type) {
            $arr_type_id = explode(",", $type);
            return $q->whereIn('type', $arr_type_id);
        }
    }

    public function scopeUserStar(Builder $q,  $user_id) {
        return $q->withCount(['is_star' => function ($q) use($user_id) {
            $q->where('user_id', $user_id);
        }]);
    }

    public function scopeUserFocus(Builder $q,  $user_id) {
        return $q->withCount(['is_focus' => function ($q) use($user_id) {
            $q->where('user_id', $user_id);
        }]);
    }

    public function scopeOrderSn(Builder $q,  $order_sn){
        if ($order_sn)
            return $q->where('order_sn', 'like', '%' . $order_sn . '%');
    }

    public function scopeMerchantId(Builder $q,  $value){
        if ($value)
            return $q->where('merchant_id', $value);
    }

    public function scopeOnStatus(Builder $q,  $value){
        if ($value != -1)
            return $q->where('on_status', $value);
    }

    public function scopeStatus(Builder $q,  $value){
        if ($value) {
            $arr_status = explode(",", $value);
            return $q->whereIn('status', $arr_status);
        }
    }

    public function scopePhone(Builder $q,  $val){
        if ($val)
            return $q->where('phone', 'like', '%' . $val . '%');
    }

    public function scopeIsNew(Builder $q,  $value){
        if ($value != -1)
            return $q->where('is_new', $value);
    }

    public function scopeUserId(Builder $q,  $value){
        if ($value)
            return $q->where('user_id', $value);
    }

    public function scopeNickname(Builder $q,  $value, $relation = ''){
        if ($relation){
            return $q->whereHas($relation, function ($qr) use($value) {
                $qr->where('nickname', 'like', '%'.$value.'%');
            });
        }
        else {
            if ($value)
                return $q->where('nickname', 'like', '%' . $value . '%');
        }
    }
}
